package com.viphuli.app.tool.handler;

import java.util.Date;
import java.util.List;

import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import butterknife.ButterKnife;

import com.offroader.utils.DateUtils;
import com.offroader.utils.ViewUtils;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.adapter.ArrangeUserStatisticsAdapter;
import com.viphuli.app.tool.bean.page.ArrangeViewMonthPage;
import com.viphuli.app.tool.bean.part.ArrangeType;
import com.viphuli.app.tool.bean.part.ArrangeView;
import com.viphuli.app.tool.bean.part.ArrangeViewDay;
import com.viphuli.app.tool.bean.part.ArrangeViewUserStatistics;
import com.viphuli.app.tool.fragment.ArrangeViewMonthFragment;

public class ArrangeMonthViewResponseHandler
		extends
		MyBaseHttpResponseHandler<ArrangeViewMonthFragment, ArrangeViewMonthPage> {

	@Override
	public void deal() {

		long time = page.getTime();

		// 移除已存在的月视图元素
		caller.getViewWeekLayout().removeAllViews();
		// 初始化月视图头
		initViewWeekHeader(time);
		// 初始化月视图数据
		initViewWeekRows(page.getArrangeViewList(), time);
		// 初始化统计
		initStatistics();

	}

	private void initStatistics() {

		List<ArrangeViewUserStatistics> statisticsList = page
				.getStatisticsList();

		if (statisticsList != null && !statisticsList.isEmpty()) {
			BaseAdapter adapter = new ArrangeUserStatisticsAdapter(
					statisticsList);
			caller.getStatisticsListview().setAdapter(adapter);

		} else {
		}

	}

	private void initViewWeekHeader(long time) {

		// 创建头
		ViewGroup header = ViewUtils.inflateView(caller.getActivity(),
				R.layout.include_month_view_header);

		// 设置头
		Date date = new Date(time);
		List<Date> dateList = DateUtils.dateToWeek(date);

		for (int i = 0; i < dateList.size(); i++) {

			View curDayView = header.getChildAt(i);// 路过空白

			TextView nameText = ButterKnife.findById(curDayView,
					R.id.id_view_month_header_name);

			Date curDate = dateList.get(i);

			String[] weekDays = caller.getResources().getStringArray(
					R.array.week_days);
			String name = DateUtils.getWeekOfDate(curDate, weekDays);

			// 设置周日期
			nameText.setText(name);

		}

		// 添加头
		caller.getViewWeekLayout().addView(header);
	}

	private void initViewWeekRows(List<ArrangeView> arrangeViewList, long time) {

		int year = page.getYear();
		int month = page.getMonth();

		int weekCountofMonth = DateUtils.getWeekCountByMonth(year, month);

		// 当前用户一个月的排班数据(某月天数)
		List<ArrangeViewDay> arrangeViewDayList = page.getArrangeViewList()
				.iterator().next().getArrangeDayList();

		for (int i = 0; i < weekCountofMonth; i++) {

			ViewGroup row = ViewUtils.inflateView(caller.getActivity(),
					R.layout.include_month_view_week);

			// 当前周日期
			List<Date> dateList = DateUtils.dateToWeek(year, month, i + 1);

			for (int j = 0; j < dateList.size(); j++) {

				// 月视图某日视图
				View dayView = row.getChildAt(j);

				TextView numText = ButterKnife.findById(dayView,
						R.id.id_view_month_day_num);
				TextView workText = ButterKnife.findById(dayView,
						R.id.id_view_month_day_work);

				Date curDayDate = dateList.get(j);
				int curMonth = DateUtils.getMonthInYear(curDayDate);
				int curDay = DateUtils.getDayInMonth(curDayDate);

				numText.setText(String.valueOf(curDay));

				if (curDay - 1 <= arrangeViewDayList.size()) {// 小于实际月份的天数

					ArrangeViewDay arrangeViewDay = arrangeViewDayList
							.get(curDay - 1);
					List<ArrangeType> arrangeTypeList = arrangeViewDay
							.getArrangeTypeList();

					if (curMonth == month) {// 只显示当月天的排班信息

						if (arrangeTypeList == null
								|| arrangeTypeList.isEmpty()) {

						} else if (arrangeTypeList.size() == 1) {

							ArrangeType arrangeType = arrangeTypeList.get(0);

							workText.setTextColor(Color.parseColor(arrangeType
									.getColor()));
							workText.setText(arrangeType.getName());

						} else if (arrangeTypeList.size() == 2) {

							ArrangeType arrangeTypeOne = arrangeTypeList.get(0);
							ArrangeType arrangeTypeTwo = arrangeTypeList.get(1);

							String subName = arrangeTypeOne.getName() + "/"
									+ arrangeTypeTwo.getName();
							SpannableString spanStr = new SpannableString(
									subName);
							spanStr.setSpan(
									new ForegroundColorSpan(Color
											.parseColor(arrangeTypeOne
													.getColor())), 0, 1,
									Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
							spanStr.setSpan(
									new ForegroundColorSpan(Color
											.parseColor(arrangeTypeTwo
													.getColor())), 2, 3,
									Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

							workText.setText(spanStr);

						}

					} else {// 不是当月的日期，低调处理
						numText.setTextColor(caller.getResources().getColor(
								R.color.cl_gray_light));
					}

				}

			}

			caller.getViewWeekLayout().addView(row);
		}

	}

}
